{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "input = \"\"\"forward 2\n",
    "forward 2\n",
    "down 7\n",
    "forward 6\n",
    "down 7\n",
    "forward 4\n",
    "down 7\n",
    "up 2\n",
    "forward 4\n",
    "down 2\n",
    "down 4\n",
    "down 5\n",
    "forward 4\n",
    "up 1\n",
    "up 2\n",
    "forward 9\n",
    "forward 7\n",
    "down 1\n",
    "forward 4\n",
    "forward 7\n",
    "forward 9\n",
    "down 2\n",
    "down 3\n",
    "forward 1\n",
    "forward 4\n",
    "forward 6\n",
    "up 2\n",
    "forward 6\n",
    "down 2\n",
    "down 5\n",
    "up 4\n",
    "down 5\n",
    "forward 6\n",
    "down 7\n",
    "down 4\n",
    "up 5\n",
    "forward 9\n",
    "forward 5\n",
    "down 8\n",
    "forward 3\n",
    "forward 1\n",
    "down 7\n",
    "down 5\n",
    "forward 2\n",
    "down 3\n",
    "forward 8\n",
    "down 1\n",
    "forward 4\n",
    "up 8\n",
    "up 8\n",
    "up 8\n",
    "forward 2\n",
    "down 3\n",
    "down 5\n",
    "up 8\n",
    "forward 1\n",
    "forward 3\n",
    "up 1\n",
    "up 2\n",
    "up 4\n",
    "down 8\n",
    "forward 5\n",
    "down 9\n",
    "down 9\n",
    "forward 9\n",
    "up 3\n",
    "down 2\n",
    "down 9\n",
    "up 8\n",
    "forward 3\n",
    "up 2\n",
    "down 7\n",
    "forward 9\n",
    "forward 1\n",
    "up 1\n",
    "forward 9\n",
    "forward 1\n",
    "down 1\n",
    "forward 7\n",
    "up 3\n",
    "up 5\n",
    "forward 3\n",
    "forward 8\n",
    "forward 4\n",
    "down 3\n",
    "forward 5\n",
    "forward 4\n",
    "forward 2\n",
    "down 2\n",
    "down 3\n",
    "forward 1\n",
    "up 2\n",
    "up 2\n",
    "down 9\n",
    "down 6\n",
    "down 6\n",
    "up 2\n",
    "down 6\n",
    "forward 6\n",
    "forward 5\n",
    "down 8\n",
    "up 3\n",
    "up 2\n",
    "up 1\n",
    "forward 8\n",
    "down 3\n",
    "down 5\n",
    "forward 8\n",
    "forward 7\n",
    "down 9\n",
    "down 7\n",
    "down 9\n",
    "down 2\n",
    "forward 8\n",
    "down 9\n",
    "forward 2\n",
    "forward 2\n",
    "down 5\n",
    "up 8\n",
    "forward 9\n",
    "down 7\n",
    "down 9\n",
    "forward 2\n",
    "forward 9\n",
    "forward 1\n",
    "forward 3\n",
    "forward 1\n",
    "forward 3\n",
    "down 4\n",
    "forward 6\n",
    "forward 4\n",
    "down 7\n",
    "up 4\n",
    "up 5\n",
    "forward 1\n",
    "down 4\n",
    "down 8\n",
    "down 3\n",
    "down 8\n",
    "up 3\n",
    "forward 2\n",
    "up 7\n",
    "down 1\n",
    "down 7\n",
    "down 3\n",
    "forward 3\n",
    "forward 4\n",
    "up 8\n",
    "down 2\n",
    "forward 8\n",
    "down 8\n",
    "up 8\n",
    "up 5\n",
    "forward 5\n",
    "forward 1\n",
    "forward 5\n",
    "up 4\n",
    "down 5\n",
    "down 1\n",
    "forward 3\n",
    "forward 4\n",
    "down 8\n",
    "down 9\n",
    "forward 4\n",
    "down 5\n",
    "down 5\n",
    "forward 9\n",
    "forward 7\n",
    "down 3\n",
    "down 4\n",
    "forward 2\n",
    "down 7\n",
    "down 3\n",
    "down 9\n",
    "down 8\n",
    "forward 6\n",
    "forward 3\n",
    "forward 1\n",
    "forward 5\n",
    "down 4\n",
    "down 4\n",
    "forward 9\n",
    "down 9\n",
    "down 5\n",
    "up 8\n",
    "forward 1\n",
    "forward 3\n",
    "down 1\n",
    "down 4\n",
    "forward 1\n",
    "up 5\n",
    "down 9\n",
    "forward 8\n",
    "down 8\n",
    "down 2\n",
    "down 3\n",
    "forward 2\n",
    "forward 9\n",
    "forward 7\n",
    "up 3\n",
    "down 3\n",
    "forward 4\n",
    "down 8\n",
    "forward 7\n",
    "down 3\n",
    "forward 3\n",
    "down 5\n",
    "forward 8\n",
    "forward 7\n",
    "forward 4\n",
    "down 2\n",
    "up 5\n",
    "forward 9\n",
    "forward 8\n",
    "down 7\n",
    "up 5\n",
    "up 8\n",
    "down 4\n",
    "down 9\n",
    "forward 5\n",
    "up 6\n",
    "forward 7\n",
    "down 4\n",
    "forward 8\n",
    "up 4\n",
    "up 2\n",
    "down 8\n",
    "forward 4\n",
    "down 8\n",
    "down 3\n",
    "down 3\n",
    "forward 1\n",
    "up 7\n",
    "forward 5\n",
    "forward 1\n",
    "down 5\n",
    "forward 4\n",
    "down 3\n",
    "down 4\n",
    "forward 4\n",
    "down 5\n",
    "up 9\n",
    "forward 6\n",
    "down 6\n",
    "up 6\n",
    "down 8\n",
    "forward 2\n",
    "down 9\n",
    "down 8\n",
    "forward 7\n",
    "down 2\n",
    "forward 1\n",
    "forward 4\n",
    "down 7\n",
    "forward 1\n",
    "up 8\n",
    "forward 6\n",
    "forward 2\n",
    "forward 5\n",
    "up 1\n",
    "forward 6\n",
    "up 7\n",
    "down 8\n",
    "down 5\n",
    "down 2\n",
    "forward 4\n",
    "forward 3\n",
    "down 7\n",
    "up 5\n",
    "forward 1\n",
    "forward 2\n",
    "forward 3\n",
    "forward 6\n",
    "down 9\n",
    "down 2\n",
    "forward 7\n",
    "up 3\n",
    "down 4\n",
    "forward 3\n",
    "forward 3\n",
    "forward 6\n",
    "up 2\n",
    "down 4\n",
    "forward 8\n",
    "down 3\n",
    "down 4\n",
    "forward 1\n",
    "forward 3\n",
    "forward 8\n",
    "forward 1\n",
    "forward 6\n",
    "forward 1\n",
    "forward 7\n",
    "down 8\n",
    "up 3\n",
    "up 5\n",
    "forward 4\n",
    "forward 6\n",
    "down 3\n",
    "forward 5\n",
    "forward 5\n",
    "down 9\n",
    "forward 4\n",
    "down 5\n",
    "forward 6\n",
    "down 1\n",
    "forward 2\n",
    "forward 3\n",
    "forward 2\n",
    "up 4\n",
    "forward 9\n",
    "forward 7\n",
    "forward 6\n",
    "up 1\n",
    "down 6\n",
    "down 1\n",
    "forward 5\n",
    "up 4\n",
    "up 9\n",
    "forward 5\n",
    "forward 3\n",
    "up 9\n",
    "down 4\n",
    "forward 7\n",
    "up 9\n",
    "down 7\n",
    "forward 5\n",
    "down 4\n",
    "down 9\n",
    "up 4\n",
    "forward 2\n",
    "forward 2\n",
    "forward 1\n",
    "forward 3\n",
    "forward 4\n",
    "down 3\n",
    "down 5\n",
    "up 9\n",
    "forward 8\n",
    "up 5\n",
    "down 3\n",
    "forward 5\n",
    "down 4\n",
    "forward 1\n",
    "forward 1\n",
    "up 5\n",
    "down 7\n",
    "up 7\n",
    "down 3\n",
    "forward 7\n",
    "down 3\n",
    "down 3\n",
    "down 5\n",
    "down 2\n",
    "down 7\n",
    "down 5\n",
    "down 1\n",
    "up 3\n",
    "forward 5\n",
    "forward 9\n",
    "up 1\n",
    "down 8\n",
    "up 5\n",
    "forward 4\n",
    "down 5\n",
    "forward 5\n",
    "down 3\n",
    "up 8\n",
    "forward 2\n",
    "up 7\n",
    "down 4\n",
    "forward 1\n",
    "up 6\n",
    "down 7\n",
    "down 4\n",
    "forward 9\n",
    "forward 3\n",
    "forward 7\n",
    "up 5\n",
    "down 1\n",
    "down 3\n",
    "up 2\n",
    "forward 3\n",
    "down 8\n",
    "up 7\n",
    "forward 9\n",
    "forward 9\n",
    "forward 9\n",
    "down 3\n",
    "forward 4\n",
    "forward 9\n",
    "down 6\n",
    "up 1\n",
    "up 7\n",
    "down 3\n",
    "forward 4\n",
    "forward 9\n",
    "down 9\n",
    "up 4\n",
    "down 2\n",
    "forward 6\n",
    "forward 4\n",
    "down 5\n",
    "up 3\n",
    "down 6\n",
    "up 4\n",
    "down 5\n",
    "down 7\n",
    "down 3\n",
    "up 1\n",
    "down 2\n",
    "forward 1\n",
    "forward 8\n",
    "up 7\n",
    "forward 4\n",
    "down 5\n",
    "down 3\n",
    "up 7\n",
    "forward 1\n",
    "forward 7\n",
    "up 6\n",
    "forward 4\n",
    "forward 5\n",
    "forward 3\n",
    "forward 1\n",
    "down 5\n",
    "up 3\n",
    "up 3\n",
    "down 8\n",
    "forward 1\n",
    "forward 2\n",
    "up 6\n",
    "down 3\n",
    "down 6\n",
    "down 5\n",
    "down 4\n",
    "up 1\n",
    "down 9\n",
    "forward 3\n",
    "down 8\n",
    "up 9\n",
    "down 3\n",
    "forward 6\n",
    "down 8\n",
    "forward 2\n",
    "forward 4\n",
    "forward 6\n",
    "forward 3\n",
    "forward 6\n",
    "up 6\n",
    "up 3\n",
    "down 6\n",
    "up 1\n",
    "forward 9\n",
    "forward 2\n",
    "up 6\n",
    "forward 7\n",
    "down 5\n",
    "forward 5\n",
    "forward 7\n",
    "down 6\n",
    "down 9\n",
    "forward 9\n",
    "forward 1\n",
    "up 4\n",
    "forward 3\n",
    "forward 2\n",
    "forward 2\n",
    "up 4\n",
    "down 7\n",
    "down 2\n",
    "up 2\n",
    "down 2\n",
    "up 9\n",
    "forward 4\n",
    "forward 3\n",
    "forward 3\n",
    "up 4\n",
    "down 7\n",
    "forward 5\n",
    "down 5\n",
    "forward 1\n",
    "down 4\n",
    "forward 4\n",
    "up 1\n",
    "down 5\n",
    "forward 2\n",
    "up 3\n",
    "up 1\n",
    "down 9\n",
    "up 9\n",
    "down 3\n",
    "forward 1\n",
    "down 6\n",
    "forward 8\n",
    "down 5\n",
    "forward 7\n",
    "down 4\n",
    "forward 2\n",
    "down 3\n",
    "down 1\n",
    "forward 6\n",
    "forward 4\n",
    "down 9\n",
    "forward 3\n",
    "up 4\n",
    "up 8\n",
    "down 2\n",
    "up 5\n",
    "up 8\n",
    "up 7\n",
    "down 2\n",
    "down 9\n",
    "forward 8\n",
    "down 4\n",
    "up 7\n",
    "forward 2\n",
    "up 4\n",
    "forward 7\n",
    "down 3\n",
    "down 3\n",
    "down 6\n",
    "up 3\n",
    "down 6\n",
    "down 6\n",
    "up 7\n",
    "down 6\n",
    "up 9\n",
    "down 6\n",
    "forward 3\n",
    "forward 8\n",
    "forward 3\n",
    "down 9\n",
    "forward 6\n",
    "forward 8\n",
    "down 7\n",
    "forward 3\n",
    "forward 7\n",
    "forward 3\n",
    "forward 8\n",
    "forward 6\n",
    "down 9\n",
    "up 4\n",
    "forward 1\n",
    "forward 6\n",
    "forward 2\n",
    "forward 4\n",
    "down 4\n",
    "down 9\n",
    "forward 7\n",
    "forward 4\n",
    "forward 4\n",
    "up 3\n",
    "up 6\n",
    "forward 4\n",
    "forward 7\n",
    "forward 4\n",
    "down 2\n",
    "forward 8\n",
    "up 4\n",
    "forward 8\n",
    "up 2\n",
    "up 4\n",
    "down 5\n",
    "forward 5\n",
    "up 2\n",
    "up 8\n",
    "up 9\n",
    "forward 6\n",
    "forward 2\n",
    "up 9\n",
    "forward 5\n",
    "up 3\n",
    "forward 8\n",
    "forward 8\n",
    "down 5\n",
    "forward 6\n",
    "up 6\n",
    "down 6\n",
    "down 1\n",
    "forward 2\n",
    "down 1\n",
    "down 5\n",
    "forward 2\n",
    "down 3\n",
    "up 7\n",
    "forward 6\n",
    "up 9\n",
    "down 3\n",
    "up 3\n",
    "forward 3\n",
    "down 9\n",
    "down 4\n",
    "down 2\n",
    "forward 7\n",
    "forward 2\n",
    "down 4\n",
    "forward 5\n",
    "up 1\n",
    "down 6\n",
    "down 6\n",
    "up 2\n",
    "down 2\n",
    "down 4\n",
    "down 4\n",
    "forward 8\n",
    "down 3\n",
    "down 5\n",
    "down 3\n",
    "up 2\n",
    "down 6\n",
    "down 9\n",
    "up 8\n",
    "forward 3\n",
    "down 9\n",
    "down 6\n",
    "down 7\n",
    "down 6\n",
    "forward 9\n",
    "up 1\n",
    "forward 9\n",
    "down 8\n",
    "forward 8\n",
    "up 5\n",
    "down 8\n",
    "forward 5\n",
    "down 6\n",
    "down 9\n",
    "forward 1\n",
    "up 5\n",
    "up 5\n",
    "forward 4\n",
    "forward 1\n",
    "forward 6\n",
    "down 1\n",
    "down 4\n",
    "down 3\n",
    "down 1\n",
    "down 1\n",
    "down 9\n",
    "down 3\n",
    "forward 6\n",
    "up 9\n",
    "up 4\n",
    "down 1\n",
    "up 6\n",
    "forward 3\n",
    "down 2\n",
    "down 8\n",
    "forward 1\n",
    "forward 8\n",
    "forward 4\n",
    "down 4\n",
    "down 5\n",
    "up 5\n",
    "up 5\n",
    "forward 7\n",
    "forward 6\n",
    "forward 4\n",
    "down 5\n",
    "forward 7\n",
    "down 4\n",
    "forward 2\n",
    "forward 5\n",
    "forward 3\n",
    "forward 5\n",
    "up 6\n",
    "up 9\n",
    "down 6\n",
    "forward 8\n",
    "down 3\n",
    "up 2\n",
    "forward 8\n",
    "down 9\n",
    "forward 9\n",
    "down 3\n",
    "down 4\n",
    "down 5\n",
    "up 3\n",
    "forward 6\n",
    "forward 6\n",
    "forward 8\n",
    "up 6\n",
    "up 4\n",
    "forward 2\n",
    "down 1\n",
    "down 5\n",
    "forward 1\n",
    "forward 8\n",
    "up 5\n",
    "down 2\n",
    "forward 4\n",
    "down 9\n",
    "up 5\n",
    "down 8\n",
    "forward 6\n",
    "forward 3\n",
    "down 8\n",
    "up 1\n",
    "down 5\n",
    "down 6\n",
    "forward 2\n",
    "forward 7\n",
    "down 8\n",
    "forward 2\n",
    "forward 8\n",
    "forward 1\n",
    "down 1\n",
    "down 3\n",
    "down 8\n",
    "down 1\n",
    "forward 1\n",
    "forward 8\n",
    "up 4\n",
    "up 5\n",
    "forward 2\n",
    "forward 5\n",
    "forward 5\n",
    "forward 9\n",
    "forward 2\n",
    "forward 4\n",
    "forward 4\n",
    "down 1\n",
    "up 8\n",
    "forward 3\n",
    "down 9\n",
    "forward 9\n",
    "up 5\n",
    "forward 7\n",
    "forward 3\n",
    "up 5\n",
    "up 5\n",
    "down 9\n",
    "forward 9\n",
    "up 2\n",
    "forward 1\n",
    "forward 1\n",
    "down 5\n",
    "down 7\n",
    "forward 2\n",
    "up 8\n",
    "up 7\n",
    "down 4\n",
    "down 6\n",
    "down 6\n",
    "down 6\n",
    "forward 4\n",
    "forward 7\n",
    "down 6\n",
    "forward 3\n",
    "forward 5\n",
    "down 2\n",
    "up 3\n",
    "forward 3\n",
    "forward 6\n",
    "forward 9\n",
    "forward 7\n",
    "forward 6\n",
    "down 4\n",
    "down 9\n",
    "forward 4\n",
    "forward 4\n",
    "down 8\n",
    "down 7\n",
    "down 1\n",
    "down 6\n",
    "up 7\n",
    "forward 3\n",
    "forward 9\n",
    "forward 9\n",
    "up 7\n",
    "forward 2\n",
    "up 5\n",
    "up 6\n",
    "down 7\n",
    "down 7\n",
    "down 5\n",
    "forward 8\n",
    "down 1\n",
    "up 4\n",
    "down 4\n",
    "forward 4\n",
    "forward 3\n",
    "forward 7\n",
    "forward 4\n",
    "down 6\n",
    "down 3\n",
    "down 3\n",
    "forward 6\n",
    "forward 7\n",
    "forward 6\n",
    "up 5\n",
    "down 9\n",
    "forward 9\n",
    "forward 5\n",
    "up 5\n",
    "down 9\n",
    "up 8\n",
    "down 6\n",
    "down 8\n",
    "down 8\n",
    "down 8\n",
    "forward 2\n",
    "down 8\n",
    "down 4\n",
    "down 6\n",
    "forward 8\n",
    "forward 3\n",
    "forward 2\n",
    "down 5\n",
    "down 3\n",
    "forward 1\n",
    "forward 2\n",
    "up 9\n",
    "forward 6\n",
    "down 8\n",
    "up 2\n",
    "down 2\n",
    "forward 7\n",
    "up 6\n",
    "up 2\n",
    "forward 1\n",
    "down 2\n",
    "forward 7\n",
    "forward 4\n",
    "down 6\n",
    "forward 4\n",
    "up 4\n",
    "forward 3\n",
    "down 2\n",
    "down 5\n",
    "up 5\n",
    "down 2\n",
    "down 1\n",
    "down 9\n",
    "forward 3\n",
    "down 5\n",
    "up 8\n",
    "down 6\n",
    "forward 3\n",
    "forward 9\n",
    "up 6\n",
    "down 2\n",
    "forward 6\n",
    "forward 7\n",
    "down 1\n",
    "up 9\n",
    "up 9\n",
    "up 9\n",
    "up 4\n",
    "forward 2\n",
    "down 7\n",
    "down 1\n",
    "forward 8\n",
    "down 2\n",
    "down 1\n",
    "forward 9\n",
    "forward 8\n",
    "up 8\n",
    "down 5\n",
    "forward 8\n",
    "down 1\n",
    "down 4\n",
    "down 3\n",
    "down 1\n",
    "forward 2\n",
    "down 2\n",
    "down 6\n",
    "forward 5\n",
    "down 9\n",
    "forward 8\n",
    "down 9\n",
    "up 6\n",
    "up 5\n",
    "forward 8\n",
    "forward 3\n",
    "down 9\n",
    "up 6\n",
    "down 8\n",
    "down 3\n",
    "down 2\n",
    "up 1\n",
    "up 9\n",
    "up 7\n",
    "up 1\n",
    "up 4\n",
    "forward 7\n",
    "down 8\n",
    "up 3\n",
    "forward 1\n",
    "up 3\n",
    "up 7\n",
    "forward 4\n",
    "up 3\n",
    "down 1\n",
    "forward 3\n",
    "forward 4\n",
    "forward 7\n",
    "down 4\n",
    "down 4\n",
    "down 2\n",
    "up 1\n",
    "up 5\n",
    "up 3\n",
    "forward 6\n",
    "forward 6\n",
    "forward 9\n",
    "down 9\n",
    "forward 2\n",
    "up 3\n",
    "forward 3\n",
    "down 2\n",
    "down 4\n",
    "forward 6\n",
    "forward 7\n",
    "down 5\n",
    "forward 9\n",
    "up 7\n",
    "down 9\n",
    "down 1\n",
    "down 1\n",
    "forward 1\n",
    "up 1\n",
    "up 5\n",
    "down 5\n",
    "forward 4\n",
    "up 3\n",
    "down 1\n",
    "down 3\n",
    "forward 3\n",
    "forward 6\n",
    "up 4\n",
    "down 2\n",
    "forward 1\n",
    "forward 5\n",
    "forward 3\n",
    "forward 2\n",
    "forward 4\n",
    "down 3\n",
    "forward 2\n",
    "down 4\n",
    "down 7\n",
    "up 4\n",
    "down 7\n",
    "forward 8\n",
    "forward 2\n",
    "down 4\n",
    "down 2\n",
    "forward 8\n",
    "up 7\n",
    "forward 1\n",
    "up 4\n",
    "forward 4\n",
    "up 7\n",
    "forward 2\n",
    "forward 7\n",
    "forward 9\n",
    "down 8\n",
    "down 2\n",
    "forward 8\n",
    "down 3\n",
    "up 5\n",
    "forward 3\n",
    "up 9\n",
    "forward 8\n",
    "forward 5\n",
    "up 2\n",
    "down 5\n",
    "down 9\n",
    "up 9\n",
    "forward 1\n",
    "forward 3\n",
    "down 3\n",
    "down 6\n",
    "forward 9\n",
    "forward 5\n",
    "down 4\n",
    "up 7\n",
    "forward 4\n",
    "forward 3\n",
    "down 2\n",
    "up 1\n",
    "forward 3\n",
    "down 4\n",
    "down 8\n",
    "up 4\n",
    "down 9\n",
    "forward 6\n",
    "down 7\n",
    "down 3\n",
    "down 9\n",
    "forward 3\n",
    "down 8\n",
    "forward 7\n",
    "forward 7\n",
    "down 1\n",
    "down 1\n",
    "forward 1\n",
    "down 4\n",
    "forward 8\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>direction</th>\n",
       "      <th>amount</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>forward</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>forward</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>down</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>forward</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>down</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>995</th>\n",
       "      <td>down</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>996</th>\n",
       "      <td>down</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>997</th>\n",
       "      <td>forward</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>998</th>\n",
       "      <td>down</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>999</th>\n",
       "      <td>forward</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1000 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "    direction  amount\n",
       "0     forward       2\n",
       "1     forward       2\n",
       "2        down       7\n",
       "3     forward       6\n",
       "4        down       7\n",
       "..        ...     ...\n",
       "995      down       1\n",
       "996      down       1\n",
       "997   forward       1\n",
       "998      down       4\n",
       "999   forward       8\n",
       "\n",
       "[1000 rows x 2 columns]"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = pd.DataFrame([i.split() for i in input.split(\"\\n\")], columns = [\"direction\", \"amount\"])\n",
    "data[\"amount\"] = pd.to_numeric(data[\"amount\"])\n",
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Part 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "direction\n",
       "down       1864\n",
       "forward    1971\n",
       "up         1034\n",
       "Name: amount, dtype: int64"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grouped = data.groupby(\"direction\")[\"amount\"].sum()\n",
    "grouped"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1635930"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(grouped[\"down\"] - grouped[\"up\"]) * grouped[\"forward\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Part 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>direction</th>\n",
       "      <th>amount</th>\n",
       "      <th>aim</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>forward</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>forward</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>down</td>\n",
       "      <td>7</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>forward</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>down</td>\n",
       "      <td>7</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>995</th>\n",
       "      <td>down</td>\n",
       "      <td>1</td>\n",
       "      <td>825</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>996</th>\n",
       "      <td>down</td>\n",
       "      <td>1</td>\n",
       "      <td>826</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>997</th>\n",
       "      <td>forward</td>\n",
       "      <td>1</td>\n",
       "      <td>826</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>998</th>\n",
       "      <td>down</td>\n",
       "      <td>4</td>\n",
       "      <td>830</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>999</th>\n",
       "      <td>forward</td>\n",
       "      <td>8</td>\n",
       "      <td>830</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1000 rows × 3 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "    direction  amount  aim\n",
       "0     forward       2    0\n",
       "1     forward       2    0\n",
       "2        down       7    7\n",
       "3     forward       6    7\n",
       "4        down       7   14\n",
       "..        ...     ...  ...\n",
       "995      down       1  825\n",
       "996      down       1  826\n",
       "997   forward       1  826\n",
       "998      down       4  830\n",
       "999   forward       8  830\n",
       "\n",
       "[1000 rows x 3 columns]"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "aim_key = {\"forward\":0, \"down\":1, \"up\":-1}\n",
    "data[\"aim\"] = (data[\"amount\"] * data.direction.map(aim_key)).cumsum()\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>direction</th>\n",
       "      <th>amount</th>\n",
       "      <th>aim</th>\n",
       "      <th>horizontal</th>\n",
       "      <th>vertical</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>forward</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>forward</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>4.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>down</td>\n",
       "      <td>7</td>\n",
       "      <td>7</td>\n",
       "      <td>4.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>forward</td>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "      <td>10.0</td>\n",
       "      <td>42.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>down</td>\n",
       "      <td>7</td>\n",
       "      <td>14</td>\n",
       "      <td>10.0</td>\n",
       "      <td>42.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>995</th>\n",
       "      <td>down</td>\n",
       "      <td>1</td>\n",
       "      <td>825</td>\n",
       "      <td>1962.0</td>\n",
       "      <td>896552.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>996</th>\n",
       "      <td>down</td>\n",
       "      <td>1</td>\n",
       "      <td>826</td>\n",
       "      <td>1962.0</td>\n",
       "      <td>896552.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>997</th>\n",
       "      <td>forward</td>\n",
       "      <td>1</td>\n",
       "      <td>826</td>\n",
       "      <td>1963.0</td>\n",
       "      <td>897378.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>998</th>\n",
       "      <td>down</td>\n",
       "      <td>4</td>\n",
       "      <td>830</td>\n",
       "      <td>1963.0</td>\n",
       "      <td>897378.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>999</th>\n",
       "      <td>forward</td>\n",
       "      <td>8</td>\n",
       "      <td>830</td>\n",
       "      <td>1971.0</td>\n",
       "      <td>904018.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1000 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "    direction  amount  aim  horizontal  vertical\n",
       "0     forward       2    0         2.0       0.0\n",
       "1     forward       2    0         4.0       0.0\n",
       "2        down       7    7         4.0       0.0\n",
       "3     forward       6    7        10.0      42.0\n",
       "4        down       7   14        10.0      42.0\n",
       "..        ...     ...  ...         ...       ...\n",
       "995      down       1  825      1962.0  896552.0\n",
       "996      down       1  826      1962.0  896552.0\n",
       "997   forward       1  826      1963.0  897378.0\n",
       "998      down       4  830      1963.0  897378.0\n",
       "999   forward       8  830      1971.0  904018.0\n",
       "\n",
       "[1000 rows x 5 columns]"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[\"horizontal\"] = data.query(\"direction == 'forward'\")[\"amount\"].cumsum()\n",
    "data[\"horizontal\"].ffill(inplace=True)\n",
    "\n",
    "data[\"vertical\"] = (data.query(\"direction == 'forward'\")[\"amount\"] * data[\"aim\"].shift(fill_value=0)).cumsum()\n",
    "data[\"vertical\"].ffill(inplace=True)\n",
    "\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1781819478.0"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.iloc[-1][[\"horizontal\", \"vertical\"]].prod()"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "758146b731c3fd8b39ea7cab27c121b86244c5bbc1f629f2f080e03f5074f425"
  },
  "kernelspec": {
   "display_name": "Python 3.9.7 64-bit ('fdap': conda)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
